// pages/myOrderAfterSale/myOrderAfterSale.js 售后详情
let url_js = require("../../utils/url.js");
let common_js = require("../../utils/common.js");
let const_js = require("../../utils/const.js");
let user_js = require("../../utils/user.js");
let ubt_js = require("../../utils/ubt.js");
let name_js = require("../../utils/ubtButtonName.js");
let api = require("../../utils/apiRequest.js");

Page({

  /**
   * 页面的初始数据
   */
  data: {
    orderId: '',
    userInfo: null,
    contentStatus: -1,
    item: null
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    //业务埋点
    ubt_js.myPageView(common_js.currentPageName(), options);

    //0.禁止转发
    wx.hideShareMenu();
    //1.初始化数据
    this.initData(options);
    //2.请求数据
    this.requestToGetAfterSale(this.data.orderId);
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {
    // 业务埋点
    ubt_js.myPageViewOut(common_js.currentPageName());
  },

  //初始化数据
  initData: function(options) {
    //1.更新userInfo
    let tempUserInfo = user_js.userInfo();
    let tmpOrderId = options.orderId ? options.orderId : '';

    this.setData({
      orderId: tmpOrderId,
      userInfo: tempUserInfo
    });
  },

  //请求售后详情的数据
  requestToGetAfterSale: function(orderId) {
    let data = {
      cityId: this.data.userInfo.ztd.cityId,
      code: user_js.getCode(),
      orderId: orderId,
      shopId: this.data.userInfo.ztd.id
    }
    api.wxRequest(url_js.orderAfterSaleUrl, data).then(res => {
      this.handleAfterSaleData(res);
    }).catch(err => {
      common_js.log(err);
    });
  },

  //处理返回的数据
  handleAfterSaleData: function(res) {
    let tmpItem = res.result;
    tmpItem.aftersaleInfoListVos.forEach(item => {
      // 售后失败
      if (item.afterStatus == 14) {
        item.afterStatusText = "售后失败";
      } else {
        item.afterStatusText = "售后";
        //
        if (item.afterSubStatus == 11) {
          item.afterSubStatusText = "待退回";
        } else if (item.afterSubStatus == 12) {
          item.afterSubStatusText = "待返仓";
        } else if (item.afterSubStatus == 13) {
          item.afterSubStatusText = "待退款";
        } else if (item.afterSubStatus == 20) {
          item.afterSubStatusText = "退款中";
        } else if (item.afterSubStatus == 21) {
          item.afterSubStatusText = "已退款";
        } else if (item.afterSubStatus == 22) {
          item.afterSubStatusText = "退款失败";
        }
      }

      //计算退款金额
      let afterSkuMoney = 0
      item.afterSkuListVo.forEach(skuItem => {
        afterSkuMoney += skuItem.refundAmount;
      });
      item.afterSkuMoney = afterSkuMoney;
    });

    //set data
    this.setData({
      contentStatus: 1,
      item: tmpItem
    })
  },

  //联系客服
  onContact: function() {
    wx.navigateTo({
      url: '../service/service'
    });
  }
})